package operator.filter;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.functions.Consumer;
import util.Printer;
import util.RxJavaHelper;
import util.ThreadHelper;

class SkipOperator {

    public static void main(String[] args) {
//        skip1();
//        skip2();
        skipLast1();
//        skipLast2();
    }

    /**
     * 跳过正序的前两项
     */
    static void skip1() {
        Observable.just(1, 2, 3, 4, 5)
                .skip(2)
                .subscribe(RxJavaHelper.getConsumer());
    }

    /**
     * 忽略指定时间内上游产生的数据
     */
    static void skip2() {
        Observable.interval(1, TimeUnit.SECONDS)
                .skip(10, TimeUnit.SECONDS)
                .subscribe(RxJavaHelper.getConsumer());
        ThreadHelper.sleep();
    }


    /**
     * 过滤正序的后两项
     * 延迟原始Observable发射的任何数据项，直到它发射了N项数据
     */
    static void skipLast1() {
        Observable.just(1, 2, 3, 4, 5)
                .doOnNext(new Consumer<Integer>() {
                    @Override
                    public void accept(Integer integer) throws Exception {
                        Printer.print("------------------------doOnNext integer = " + integer);
                    }
                })
                .skipLast(2)
                .subscribe(RxJavaHelper.getConsumer());
    }

    /**
     * 过滤掉后面指定时间内的数据
     * 延迟原始Observable发射的任何数据项，直到自这次发射之后过了给定的时长
     */
    static void skipLast2() {
        Observable.interval(1, TimeUnit.SECONDS)
                .doOnNext(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Exception {
                        Printer.print("------------------------doOnNext aLong = " + aLong);
                    }
                })
                .take(10, TimeUnit.SECONDS)
                .skipLast(3, TimeUnit.SECONDS)
                .subscribe(RxJavaHelper.getConsumer());
        ThreadHelper.sleep();
    }
}
